import Account from '../dll/account'

export default {
  data () {
    return {
      phone_reg: /^1([3456789])\d{9}$/,
      email_reg: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})(\.[a-zA-Z]{2,4})?$/,
      passwd_reg: /(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![\d\W]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)(^[a-zA-Z0-9\W_]{8,16}$)/,

      minutes: 0, // 倒计时
      timer: null, // 定时器

      verify_type: '1' // 短信类型
    }
  },
  methods: {
    // 获取验证码
    async getVerifyCode () {
      if (this.find_type == 1 && !this.phone_reg.test(this.verify_account)) {
        this.$vux.toast.text('请输入正确的手机号')
        return
      }
      if (this.find_type == 2 && !this.email_reg.test(this.verify_account)) {
        this.$vux.toast.text('请输入正确的邮箱地址')
        return
      }
      if (this.timer) {
        return
      }
      this.$vux.loading.show({
        text: '获取中...'
      })

      let param = {
        verify_type: this.find_type,
        verify_account: this.verify_account
      }
      if (this.isSMS) { // 判断是不是账号激活
        param.is_active = 1
      }
      let res = await Account.getVerifyCode(param)
      this.$vux.loading.hide()
      if (res.code == 0) {
        this.starTimer()
        this.$vux.toast.text(res.msg)
      } else {
        let _this = this
        if (this.isSMS) { // 账号无法激活提示
          this.$vux.confirm.show({
            title: '提示',
            content: '请使用账号密码登录',
            confirmText: '账号密码登录',
            onConfirm () {
              _this.onAccount()
            },
            onCancel () {
            }
          })
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: res.msg
          })
        }
      }
    },

    // 倒计时
    starTimer: function () {
      this.minutes = 60
      if (this.timer == null) {
        this.timer = setInterval(() => {
          this.minutes -= 1
          this.$refs.codeBtn.innerText = this.minutes + 's重新获取'
          if (this.minutes <= 0) {
            this.minutes = 0
            this.endTimer()
            this.$refs.codeBtn.innerText = '获取验证码'
          }
        }, 1000)
      }
    },

    // 清除定时器
    endTimer: function () {
      clearInterval(this.timer)
      this.timer = null
    }
  },

  beforeDestroy () {
    this.endTimer()
  }
}
